UIWeaponLevelUpConfirm = UIBase.New(PanelResNames.UIWeaponLevelUpConfirm)

function UIWeaponLevelUpConfirm.Init(partnerId)
    UIWeaponLevelUpConfirm.partnerId = partnerId
    UIBase.Init(UIWeaponLevelUpConfirm)
end

function UIWeaponLevelUpConfirm:OnCreate()
    self.btnClose = self:GetChild("Btn_BGClose")
    self.btnCancel = self:GetChild("CancelBtn")
    self.btnConfirm = self:GetChild("ConfirmBtn")
    self.txtCurSkillDesc = self:GetChild("skillDesc")
    self.txtTargetSkillDesc = self:GetChild("skillDesc2")
    self.attrList = self:GetChild("List_WeaponAttribute")
    self.itemList = self:GetChild("listCost")
    self.iconSkill = self:GetChild("btnSkill"):GetChild("Mask"):GetChild("Icon")
    self.ctrlSkillLevelUp = self:GetController("skillLevelUp")
    self.currentWeaponLv = 0
    self.attributeShowConfig = G.dataTable["t_attributelibrary"];
    self:AddEvent()
    self:InitView()
end

function UIWeaponLevelUpConfirm:InitView()
    self.itemList.numItems = 1
    self.curAttrList, self.tarAttrList = self:GetAttrData()
    if self.curAttrList and self.tarAttrList then
        self.attrList.numItems = (#self.curAttrList + 1)
    end
    self.skillInfo = dataTable.getDataTable("t_weaponskill")[150000 + tonumber(self.currentPartnerInfo.partnerModelId)]
    if self.skillInfo then
        self.iconSkill.url = "ui://SkillAtlas/" .. G.dataTable["t_skillinfo"][tostring(self.skillInfo[20].f_WeaponSkill)].f_SkillIcon
    end

    local _, _, targetLv = self:GetLevelUpInfo()
    local curLvStageIdx = 0
    local tarLvStageIdx = 0
    for k,v in pairs(self.skillInfo) do
        if self.currentWeaponLv >= k and curLvStageIdx < k then
                curLvStageIdx = k
        end
        if targetLv >= k and tarLvStageIdx < k then
            tarLvStageIdx = k
        end
    end
    if curLvStageIdx ~= tarLvStageIdx and tarLvStageIdx > curLvStageIdx then
        self.ctrlSkillLevelUp.selectedIndex = 0
    else
        self.ctrlSkillLevelUp.selectedIndex = 1
    end
    if curLvStageIdx > 0 then
        local config = self.skillInfo[curLvStageIdx]
        self.txtCurSkillDesc.text = string.format(getLanguage("WeaponSkillText1"), config.f_WeaponLevel) .. getLanguage(config.f_WeaponSkillDesc)
    else
        self.txtCurSkillDesc.text = getLanguage("WeaponSkillText5")
    end
    if tarLvStageIdx > curLvStageIdx then
        local config = self.skillInfo[tarLvStageIdx]
        self.txtTargetSkillDesc.text = string.format(getLanguage("WeaponSkillText1"), config.f_WeaponLevel) .. getLanguage(config.f_WeaponSkillDesc)
    end
end

function UIWeaponLevelUpConfirm:AddEvent()
    self:RegisterOnClick(self.btnClose,self.OnClickBack)
    self:RegisterOnClick(self.btnCancel,self.OnClickBack)
    self:RegisterOnClick(self.btnConfirm,self.OnClickConfirm)
    self:SetDynamicList(self.attrList,self.AttrRender)
    self:SetDynamicList(self.itemList,self.ItemRender)
end

function UIWeaponLevelUpConfirm:AttrRender(index, go)
    local txtName = go:GetChild("attrText")
    local curVal = go:GetChild("num")
    local tarVal = go:GetChild("upNum")
    if index == 1 then
        txtName.text = getLanguage("Sys_HeroLevel")
        curVal.text = self.currentWeaponLv
        tarVal.text = self.targetLv
    else
        local attributeShowConfig = self.attributeShowConfig[tostring(self.curAttrList[index-1][1])]
        if attributeShowConfig.f_IsPanel == 1 then
            txtName.text = getLanguage(string.format(Config.LangAttribute, self.curAttrList[index-1][1]))
            if attributeShowConfig.f_AttributeType == 1 then
                curVal.text = self.curAttrList[index-1][2]
                tarVal.text = self.tarAttrList[index-1][2]
            elseif attributeShowConfig.f_AttributeType == 2 then
                curVal.text = (self.curAttrList[index-1][2]/100).."%"
                tarVal.text = (self.tarAttrList[index-1][2]/100).."%"
            end
        end
    end
end

function UIWeaponLevelUpConfirm:ItemRender(index, go)
    local useItemModelId, totalUseItemCount, targetLv = self:GetLevelUpInfo()
    if useItemModelId > 0 and totalUseItemCount > 0 and targetLv > self.currentWeaponLv then
        local costItemData = logicMgr.CostManager.getItemOrEquipDataByIdAndNum(useItemModelId, totalUseItemCount)
        local ctrlItem = CtrlManager.GetCtrl(PanelNames.Item)
        if costItemData and ctrlItem then
            ctrlItem.initData(go, costItemData)
        end
    end
end

--计算消耗的道具，数量，和目标等级
function UIWeaponLevelUpConfirm:GetLevelUpInfo()
    self.currentPartnerInfo = HeroManager.getHeroDatabyPartnerId(self.partnerId, true)
    if not self.currentPartnerInfo then
        return
    end
    local configId = 150000 + tonumber(self.currentPartnerInfo.partnerModelId)
    self.weaponConfig = G.dataTable["t_weapon"][tostring(configId)]
    self.currentWeaponInfo = EquipManager.tabWeapon[tostring(self.currentPartnerInfo.partnerId)]
    if self.currentWeaponInfo == nil then
        printError("UIWeaponLevelUpConfirm currentWeaponInfo is Null, partnerId " .. self.currentPartnerInfo.partnerId)
        self:OnClickBack()
        return
    end
    self.currentWeaponLv = self.currentWeaponInfo.level

    local useItemModelId = 1
    local nextNeedCount = 0
    local useItemCount = 0
    local targetLv = 0
    if self.weaponConfig["f_WeaponEnergyCost"] and self.weaponConfig["f_WeaponEnergyCost"][1] and self.weaponConfig["f_WeaponEnergyCost"][1][1] then
        useItemModelId = self.weaponConfig["f_WeaponEnergyCost"][1][1]
    end
    if self.currentWeaponLv > 0 then
        useItemModelId = self.weaponConfig["f_WeaponEnergyCost"][self.currentWeaponLv + 1][1]
    end
    nextNeedCount = self.weaponConfig["f_WeaponEnergyCost"][self.currentWeaponLv + 1][2]
    local maxLevel = #self.weaponConfig["f_WeaponEnergyCost"]
    for k,v in pairs(self.weaponConfig["f_WeaponEnergyCost"]) do
        local unlockStar = self.weaponConfig["f_WeaponNeedStar"][self.currentWeaponLv + 1]
        local currencyNum = logicMgr.ItemManager.getCurrencyCountByModelId(v[1])
        if k > self.currentWeaponLv and v[1] == useItemModelId and currencyNum >= nextNeedCount and self.currentPartnerInfo.star >= unlockStar then
            targetLv = k
            if k <= maxLevel then
				if k == maxLevel then
					useItemCount = useItemCount + self.weaponConfig["f_WeaponEnergyCost"][k][2]
				else
					nextNeedCount = nextNeedCount + self.weaponConfig["f_WeaponEnergyCost"][k+1][2]
					useItemCount = useItemCount + self.weaponConfig["f_WeaponEnergyCost"][k][2]
				end
            end
        end
    end
    self.targetLv = targetLv
    return useItemModelId, useItemCount, targetLv
end

function UIWeaponLevelUpConfirm:GetAttrData()
    local _, _, targetLv = self:GetLevelUpInfo()
    if targetLv <= self.currentWeaponLv then return end
    local curAttrCfg = self.weaponConfig["f_WeaponValue"][self.currentWeaponLv + 1]
    local tarAttrCfg = self.weaponConfig["f_WeaponValue"][targetLv + 1]
   return curAttrCfg, tarAttrCfg
end

function UIWeaponLevelUpConfirm:OnClickBack()
    closeUI(PanelNames.UIWeaponLevelUpConfirm)
end

function UIWeaponLevelUpConfirm:OnClickConfirm()
    weaponHandler.sendWeaponFastUpLevelMessage(self.partnerId, self.currentWeaponLv)
    closeUI(PanelNames.UIWeaponLevelUpConfirm)
end

return UIWeaponLevelUpConfirm